package com.stropheware.droidDefense;

import com.stropheware.pathfinding.TowerSet;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.text.TextPaint;
import android.util.Log;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Align;

public abstract class AbstractTower {
	
	public final static int TOWER_SIDE_LENGTH = 4; //needs to be even
	
	protected int numProjectiles;
	
	public final static String TAG = "AbstractTower";
	
	public AnimationDrawable animationDrawable;
	
	public Drawable drawable;
	
	public boolean isDamageMaker;
	
	protected Context context;
	
	public GameBoard gameBoard;
	
	public TowerSet currentTowerSet;
	
	public int radius; //in regards to exact center of tower in cells
	
	public int damagePerShot;
	
	public int speed;
	
	public int cost;
	
	public AbstractEnemy currentTarget;
	
	public Cell [][] cellsOccupied = new Cell[TOWER_SIDE_LENGTH][TOWER_SIDE_LENGTH];
	
	protected AbstractProjectile[]  projectiles;
	
	public int widthPixels, halfWidthPixels;
	
	/*
	 * Lander heading in degrees, with 0 up, 90 right. Kept in the range 0..2pi.
	 */
	public int heading = 0;
	
	public int topPx, leftPx;
	
	public int centerX, centerY;
	
	private int shotIndex = 0;

	/**Tower Pop Up Variables*/
	public boolean display;
	public static float popUpLeft;
	public static float popUpTop;
	public static float popUpRight;
	public static float popUpBottom;
	public static Paint popUpPaint = new Paint();
	public static Paint popUpOutlinePaint = new Paint();
	public static RectF popUp = new RectF();
	public static Drawable upgrade;
	public static Drawable sell;
	public static int widthpx;
	public static int upgradeBounds [] = new int[4];
	public static int sellBounds [] = new int[4];
	public static final int popUpWidth = 160;
	public static final int popUpHeight = 100;
	private AnimationDrawable a;
	int animCount;
	
	public AbstractTower(GameBoard gameBoard, int topPx, int leftPx) {
		this.topPx = topPx;
		this.leftPx = leftPx;
		this.centerX = leftPx + ((TOWER_SIDE_LENGTH / 2) * gameBoard.cellSideLength);
		this.centerY = topPx + ((TOWER_SIDE_LENGTH / 2) * gameBoard.cellSideLength);
		this.gameBoard = gameBoard;
		this.context = gameBoard.context;
		widthPixels = gameBoard.cellSideLength * TOWER_SIDE_LENGTH;
		halfWidthPixels = widthPixels / 2;
		currentTarget = null;
		
		display = false;
		popUpPaint.setColor(Color.TRANSPARENT);
		popUpPaint.setAntiAlias(true);	
		popUpOutlinePaint.setAntiAlias(true);
		popUpOutlinePaint.setColor(Color.WHITE);
		popUpOutlinePaint.setStyle(Paint.Style.STROKE);
		popUpOutlinePaint.setStrokeWidth(2);
		upgrade = this.gameBoard.context.getResources().getDrawable(R.drawable.upgrade);
		sell = this.gameBoard.context.getResources().getDrawable(R.drawable.sell);
		animCount = 0;
		a = new AnimationDrawable();
		a.addFrame(context.getResources().getDrawable(R.drawable.upgrade), 60);
		a.addFrame(context.getResources().getDrawable(R.drawable.sell),60);
		a.setBounds(0, 0, 100, 100);
		a.start();	
	}
	
	public AbstractTower(GameBoard gameBoard, Cell topLeftCell) {
		this(gameBoard, topLeftCell.pxRow, topLeftCell.pxColumn);
	}
	
	public AbstractTower() {

	}
	
	public void drawAnimation(Canvas canvas) {
		animationDrawable.setBounds(leftPx, topPx, leftPx + widthPixels, topPx + widthPixels);
		drawable.draw(canvas);
	}

	public void drawDrawable(Canvas canvas) {
		canvas.save();
		canvas.rotate((float) heading, leftPx + halfWidthPixels, topPx + halfWidthPixels);
		drawable.setBounds(leftPx, topPx, leftPx + widthPixels, topPx + widthPixels);
		drawable.draw(canvas);
		canvas.restore();
	}
	
	public void drawProjectiles(Canvas canvas) {
		for(int i = 0; i < numProjectiles; i++) projectiles[i].draw(canvas);
	}
	
	public void upgrade(){
		if(this instanceof BasicTower)this.upgradeToAdvanced();	
		else ;
	}
	
	protected void upgradeToAdvanced() {
		
	}

	public void sell(GameManager manager){
		manager.towerManager.removeTower(this.cellsOccupied[0][0], this);
	}

	
	public void update(int tickCount) {
		if(currentTarget != null && tickCount % speed == 0) {
			rotateAtEnemy(currentTarget);
			fireAtEnemy(currentTarget);
		}else if (currentTarget != null) {
			rotateAtEnemy(currentTarget);
			//for(int i = 0; i < numProjectiles; i++) projectiles[i].update(tickCount);
		}
		for(int i = 0; i < numProjectiles; i++) projectiles[i].update(tickCount);
	}
	
	public void analyzePotentialEnemies(AbstractEnemy [] potentialEnemies) {
		for(int i = 0; i < potentialEnemies.length; i ++) {
			AbstractEnemy temp = potentialEnemies[i];
			if(temp.visible && enemyInRange(temp)) {
				currentTarget = temp;
				return;
			}
		}
		currentTarget = null;
	}
	
	private boolean enemyInRange(AbstractEnemy enemy) {
		return (Math.sqrt (Math.pow(((enemy.xpx + (enemy.SIZE / 2 * gameBoard.cellSideLength)) - centerX),2) + Math.pow(((enemy.ypx + (enemy.SIZE / 2 * gameBoard.cellSideLength)) - centerY), 2)) <= (double)(radius));
	}

	public void fireAtEnemy(AbstractEnemy enemy) {
		projectiles[shotIndex].fireAtEnemy(enemy);
		shotIndex++;
		if(shotIndex >= numProjectiles) shotIndex = 0;
	}
	
	public void rotateAtEnemy(AbstractEnemy enemy) {
		//xe - xt, ye - yt
		double radians = Math.atan2(enemy.xpx + enemy.addToEnemyPx - centerX , enemy.ypx + enemy.addToEnemyPx - centerY);
		heading = (int) (-radians * (180 / Math.PI)) + 180;
	}

	
	public void showPopUp() {
		display = true;
		if(leftPx+popUpWidth >= gameBoard.actualWidth)AbstractTower.popUpLeft = gameBoard.actualWidth-popUpWidth-10;
		else AbstractTower.popUpLeft = leftPx;
		if(topPx+widthPixels+popUpHeight >= gameBoard.actualHeight)AbstractTower.popUpTop = topPx-popUpHeight;
		else AbstractTower.popUpTop = topPx+widthPixels;
		AbstractTower.popUpRight = AbstractTower.popUpLeft+popUpWidth;
		AbstractTower.popUpBottom = AbstractTower.popUpTop+popUpHeight;
		AbstractTower.widthpx = widthPixels;
	}	
	
	/**This draws the PopUP menu for Towers
	 * All popUp menus are the same size across all screens and devices,
	 * this can most likely be changed easily...TODO
	 * @param canvas - the canvas to draw on
	 */
	public void drawPopUp(Canvas canvas){
		if(display){
		popUpPaint.setShadowLayer(4, 0, 1, 0xbb000000);
		popUp.set(popUpLeft,popUpTop,popUpRight,popUpBottom);
		
		upgrade.setBounds((int)popUpLeft+5, (int)popUpTop+5, (int)popUpLeft+50, (int)popUpTop+50);
		sell.setBounds((int)popUpLeft+5, (int)popUpTop+50, (int)popUpLeft+50, (int)popUpTop+95);
		
		upgradeBounds[0] = upgrade.getBounds().left; 
		upgradeBounds[1] = upgrade.getBounds().top; 
		upgradeBounds[2] = upgrade.getBounds().right; 
		upgradeBounds[3] = upgrade.getBounds().bottom; 
		
		sellBounds[0] = sell.getBounds().left; 
		sellBounds[1] = sell.getBounds().top; 
		sellBounds[2] = sell.getBounds().right; 
		sellBounds[3] = sell.getBounds().bottom; 
		
		canvas.drawRoundRect(popUp, 7, 7, popUpPaint);
		popUp.set(popUpLeft+3,popUpTop+3,popUpRight-3,popUpBottom-3);
		canvas.drawRoundRect(popUp, 3, 3, popUpOutlinePaint);
		TextPaint t = new TextPaint();
		t.setAntiAlias(true);
		t.setTextSize(11);

		t.setColor(Color.WHITE);
		canvas.drawText("Damage: ", popUpLeft+55, popUpTop+20, t);
		t.setColor(0xffff4444);
		canvas.drawText(""+damagePerShot+" -- 50", popUpLeft+102, popUpTop+20, t);
		t.setColor(Color.WHITE);
		canvas.drawText("Range: ", popUpLeft+55, popUpTop+32, t);
		t.setColor(Color.GREEN);
		canvas.drawText(""+radius+" -- 100", popUpLeft+92, popUpTop+32, t);
		t.setColor(Color.WHITE);
		canvas.drawText("Cost: ", popUpLeft+55, popUpTop+44, t);
		t.setColor(Color.YELLOW);
		canvas.drawText("100g", popUpLeft+86, popUpTop+44, t);
		t.setColor(Color.WHITE);
		canvas.drawText("Sell for: ", popUpLeft+55, popUpTop+75, t);
		t.setColor(Color.YELLOW);
		canvas.drawText("100g", popUpLeft+96, popUpTop+75, t);
		upgrade.draw(canvas);
		sell.draw(canvas);
		
		
		a.selectDrawable(animCount++%2);
		a.draw(canvas);
		
		}
	}

	
}
